'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
  const flag = event.flag
  if (flag === 0) { // 查询分类ID
    let {
      name
    } = event
    return await db.collection("uni-cms-categories").where({
      name
    }).get()
  } else if (flag === 1) { //查询各个分类全部的文章
    let {
      skip,
      category_id
    } = event
    return await db.collection("uni-cms-articles").where({
      category_id
    }).orderBy("publish_date", "desc").limit(8).skip(skip).get()
  } else if (flag === 2) { // 查询文章详细内容
    let {
      id
    } = event
    return await db.collection("uni-cms-articles").doc(id).get()
  } else if (flag === 3) { // 搜索功能
    let {
      keyword,
      category_id
    } = event
    return await db.collection("uni-cms-articles").where({
      category_id,
      title: new RegExp(keyword, 'ig')
    }).get()
  }
};